package com.feige.auth.pojo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.feige.base.dto.QueryAuditorBaseDto;
import com.feige.utils.string.StringUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author feige
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class PrincipleDto extends QueryAuditorBaseDto<PrinciplePo> {

    private static final long serialVersionUID = -3253329104362619145L;

    @ApiModelProperty(value = "登录名")
    private String loginName;

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    @ApiModelProperty(value = "关联用户实体")
    private Long userId;


    @Override
    public LambdaQueryWrapper<PrinciplePo> baseQueryWrapper() {
        LambdaQueryWrapper<PrinciplePo> queryWrapper = super.baseQueryWrapper();
        String loginName = this.getLoginName();
        if (StringUtils.isNotEmpty(loginName)){
            queryWrapper.like(PrinciplePo::getLoginName, loginName);
        }
        Long userId = this.getUserId();
        if (userId != null){
            queryWrapper.ge(PrinciplePo::getUserId, userId);
        }
        return queryWrapper;
    }

    @Override
    public PrinciplePo toPo() {
        PrinciplePo principlePo = new PrinciplePo();
        principlePo.setId(this.getId());
        principlePo.setLoginName(this.getLoginName());
        principlePo.setUserId(this.getUserId());
        return principlePo;
    }
}
